package org.springblade.modules.repair.vo.elevator;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;
import org.springblade.modules.repair.enums.EmerProcTypeEnum;
import org.springblade.modules.repair.enums.WorkOrderTypeEnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2023/10/30 9:25
 */
@Data
@ApiModel("电梯工单model")
public class ElevatorPlanVo {

	@ApiModelProperty("工单id")
	private Long planId;

	@ApiModelProperty("工单状态")
	private Integer status;

	@ApiModelProperty("工单类型")
	private Integer typeStatus;


	@ApiModelProperty("工单状态")
	private String planDesc;

	@ApiModelProperty("执行人")
	private List<MaintainPlanContentUser> list;


	private Date time;

	private Date finishTime;


	//维修相关
	@ApiModelProperty("故障枚举id")
	private String problemItem;

	@ApiModelProperty("故障类型")
	private List<String> problemItemNames;

	//配件相关
	@ApiModelProperty("配件名称")
	private String partName;

	@ApiModelProperty("配件型号")
	private String partsModel;

	@ApiModelProperty("配件数量")
	private Integer partNum;


	//保养相关
	private Integer deferType;

	private Date overdueDate;

	private String overdueDateDesc;

	private String timeliness;

	private Integer auditType;

	private Date maintainSignatureTime;

	private String partsNames;

	/**
	 * 层站id集合
	 */
	private String floorStandIds;

	/**
	 * 层站名称集合
	 */
	private String floorStandNames;

	/**
	 * 紧急程度
	 */
	private Integer emerProcType;

	/**
	 * 工单类型
	 */
	private Integer workOrderType;

	/**
	 * 紧急程度描述
	 */
	private String emerProcTypeName;

	/**
	 * 工单类型描述
	 */
	private String workOrderTypeName;

	public void setEmerProcType(Integer emerProcType) {
		if (Func.isNotEmpty(emerProcType)) {
			this.emerProcTypeName = EmerProcTypeEnum.getName(emerProcType);
		}
		this.emerProcType = emerProcType;
	}

	public void setWorkOrderType(Integer workOrderType) {
		if (Func.isNotEmpty(workOrderType)) {
			this.workOrderTypeName = WorkOrderTypeEnum.getName(workOrderType);
		}
		this.workOrderType = workOrderType;
	}

	public void setPartsNames(String partsNames) {
		if (Func.isNotEmpty(partsNames)) {
			this.partsNames = partsNames.replace("&amp;&amp;", ">");
		}
	}

	public void setFloorStandNames(String floorStandNames) {
		if (Func.isNotEmpty(floorStandNames)) {
			this.floorStandNames = floorStandNames.replace("&amp;&amp;", ">");
		}
	}

	public void setTime(Date time) {
		this.time = time;
		Integer day = Convert.toInt(DateUtil.betweenDay(time, new Date(), true));
		if (day > 0) {
			this.timeliness = "超时" + day + "天";
		}
	}

	public void setPlanDesc(String planDesc) {
		if (Func.isNotEmpty(planDesc) && planDesc.contains("%")) {
			List<String> strings = Func.toStrList(planDesc);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str : strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[1]);
				Integer sort = 0;
				try {
					sort = Convert.toInt(split[2], 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
				maintainPlanTemplate.setSort(sort);
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.planDesc = String.join(",", collect);
		} else {
			this.planDesc = planDesc;
		}
	}


}
